<?php

define("IS_MAIN", true);
require "./modules/decto26.php";
require "./modules/set.php";

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

require "./modules/mail.php";

header("Content-Type: application/json;charset=utf8");
if ($_SERVER["REQUEST_METHOD"] != "GET") {
	echo '{"code":"error","message":"请求方法不正确"}';
	exit;
}
if (isset($_COOKIE["htqw_share_setup_email"]) == false) {
	echo '{"code":"error","message":"缺少COOKIE"}';
	exit;
}
$email = $_COOKIE["htqw_share_setup_email"];

$conn = mysqli_connect($app["sql"]["host"], $app["sql"]["user"], $app["sql"]["password"], $app["sql"]["dbname"], $app["sql"]["port"]);
if ($conn->connect_errno) {
	echo '{"code":"error","message":"数据库链接失败"}';
	exit;
}

if (isset($_COOKIE["htqw_share_setup_code_id"]) && ($_COOKIE["htqw_share_setup_code_id"] !== "isdeleted")) {
	$code = "";
	$stmt = $conn->prepare("select * from code where id=?");
	$stmt->bind_param("s", $_COOKIE["htqw_share_setup_code_id"]);
	$stmt->execute();
	$result = $stmt->get_result();
	if ($result->num_rows == 0) {
		mysqli_close($conn);
		echo '{"code":"error","message":"codeId错误！"}';
		setcookie("htqw_share_setup_code_id", "isdeleted", time() + 60 * 24 * 30, "/share/");
		exit;
	}
	for ($i = 0; $i < 6; $i++) {
		$code .= decToBase26(mt_rand(0, 26));
	}
	$row = $result->fetch_assoc();
	if (intval(time()) - intval($row["time"]) < 59) {
		mysqli_close($conn);
		echo '{"code":"error","message":"请' . (60 - intval(time()) + intval($row["time"])) . '秒后重试"}';
		exit;
	}
	$stmt = $conn->prepare("update code set code=?,time=? where id=?");
	$time = strval(time());
	$stmt->bind_param("sss", $code, $time, $_COOKIE["htqw_share_setup_code_id"]);
	$stmt->execute();
	$conn->close();
	$content = "<h1>您的htqw共享注册授权码是：" . $code . "，有效期10分钟，如非本人操作，请勿理会！</h1>";
	$subjectaaa = "注册授权码";
	$sm = send_mail(new PHPMailer, $subjectaaa, $content, $email, $app);
	if (!$sm) {
		mysqli_close($conn);
		echo '{"code":"error","message":"发送邮件时出现错误"}';
		exit;
	}
	echo '{"code":"success"}';
	exit;
}

$cmd = $conn->query("select count(*) as rows from code");
if (!$cmd) {
	mysqli_close($conn);
	echo '{"code":"error","message":"操作数据库时出现错误"}';
	exit;
}

$row = $cmd->fetch_assoc();
$stmt = $conn->prepare("insert into code (id, code, time) values (?, ?, ?)");
$code = "";
for ($i = 0; $i < 6; $i++) {
	$code .= decToBase26(mt_rand(0, 26));
}
$sql_id = decToBase26($row['rows']) . "";
$aaaaa = "sss";
$timee = time() . "";
$stmt->bind_param($aaaaa, $sql_id, $code, $timee);
if (!$stmt->execute()) {
	mysqli_close($conn);
	echo '{"code":"error","message":"操作数据库时出现错误"}';
	exit;
}
setcookie("htqw_share_setup_code_id", $sql_id, time() + 60 * 24 * 30, "/");

$sm = send_mail(new PHPMailer, "注册授权码", "<h1>您的htqw共享注册授权码是：" . $code . "，有效期10分钟，如非本人操作，请勿理会！</h1>", $email, $php);
if (!$sm) {
	mysqli_close($conn);
	echo '{"code":"error","message":"发送邮件时出现错误"}';
	exit;
}
$conn->close();
echo '{"code":"success"}';
